package servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

import javax.jms.JMSException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Newspaper;
import newspapers.autopublishing.AutoNewspaper;
import newspapers.autopublishing.ClientControl;
import newspapers.autopublishing.NewspaperConfig;

/**
 * Servlet implementation class PublishServlet.
 *
 * This shouldn't exist in an application for subscribers, but it is useful
 * until we have an application for publishers.
 */
public class PublishServlet extends HttpServlet {
    /** Serial version UID. */
    private static final long serialVersionUID = 1L;

    /**
     * Initializes the servlet.
     *
     * @param config  configuration data of the servlet
     * @throws ServletException  if there is any problem
     */
    @Override
    public void init(final ServletConfig config) throws ServletException {
        super.init(config);
    }

    /**
     * Launch the autopublishing newspapers (The Guardian and The Telegraph).
     *
     * Forward to the reader main view.
     *
     * @param request  HTTP request
     * @param response  HTTP response
     * @throws ServletException  if there is any problem
     * @throws IOException  if there is any problem
     * @see HttpServlet#doGet(HttpServletRequest request,
     *                           HttpServletResponse response)
     */
    @Override
    @SuppressWarnings("unchecked")
    protected void doGet(final HttpServletRequest request,
            final HttpServletResponse response)
            throws ServletException, IOException {
        //launch autopublishers
        ArrayList<NewspaperConfig> publishers = (ArrayList<NewspaperConfig>)
                getServletContext().getAttribute("pubCC");
        AutoNewspaper [] newspapers = (AutoNewspaper[])
                getServletContext().getAttribute("npCC");
        new ClientControl(publishers, newspapers);

        getServletContext().getRequestDispatcher("/jsp/readerMainView.jsp")
                .forward(request, response);
    }

    /**
     * Decide if autopublishing or non-autopublishing newspapers are requested.
     * If autopublishing: invoke doGet
     * If non-autopublishing: make The Times and The Sun publish some messages.
     *
     * @param request  HTTP request
     * @param response  HTTP response
     * @throws ServletException  if there is any problem
     * @throws IOException  if there is any problem
     * @see HttpServlet#doPost(HttpServletRequest request,
     *                            HttpServletResponse response)
     */
    @Override
    @SuppressWarnings("unchecked")
    protected void doPost(final HttpServletRequest request,
            final HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter("action");
        if (action.contains("auto")) {
            doGet(request, response);
        } else {
            //get The Times and The Sun from the context
            HashMap<String, Newspaper> npList = (HashMap<String, Newspaper>)
                    getServletContext().getAttribute("npList");
            Collection<Newspaper> newspapers = npList.values();
            Newspaper theTimes = null;
            Newspaper theSun = null;
            for (Newspaper newspaper : newspapers) {
                String name = newspaper.getName();
                if (name.equals("The Times")) {
                    theTimes = newspaper;
                } else if (name.equals("The Sun")) {
                    theSun = newspaper;
                }
            }
            try {
				//Publish some messages from The Times
				HashMap<String, String> properties1 = new HashMap<String, String>();
//				properties1.put("author", "Anna Jones");
//				properties1.put("date", "November 09");
//				properties1.put("sport", "football");
				properties1.put("header", "news1 from times-sport");
				theTimes.publishMessage("times.sport", "Message about sports from "
                        + theTimes.getName() + ". Loooooooo ooooooo ooooooo oo"
//				        + theTimes.getName() + " with properties. loooooooooooo oo"
//                    + "ooo oooooooooooooo oooooooo ooooooooooooooo oooooooo oo"
//                    + "ooooooooo oooooo ooooooooo ooooooooo oooooooo ooooooooo"
//                    + "o ooooooo ooooooo oooooooooo ooooooooo oooooooo ooooooo"
//                    + "oo ooooooo ooooooooo ooooooooo oooooooooo oooooooo oooo"
//                    + "oooo oooooooo oooooooooo oooooooooo ooooooooooooooooooo"
//                    + "oooooooooooooooooooooooooooooooooooooo oooooooooooooooo"
				        + "ooooong message.", properties1);
				HashMap<String, String> properties3 = new HashMap<String, String>();
				properties3.put("header", "news1 from times-music");
				theTimes.publishMessage("times.music", "Message about music from "
				        + theTimes.getName(), properties3);
				HashMap<String, String> properties4 = new HashMap<String, String>();
				properties4.put("header", "news2 from times-sport");
				theTimes.publishMessage("times.sport",
				        "Another message about sports from "
				        + theTimes.getName(), properties4);
				HashMap<String, String> properties2 = new HashMap<String, String>();
//				properties2.put("author", "Harry Williams");
//				properties2.put("date", "November 09");
//				properties2.put("arts", "photography");
				properties2.put("header", "news1 from times-arts");
				theTimes.publishMessage("times.arts", "Message about arts from "
				        + theTimes.getName(), properties2);
				//Publish some messages from The Sun
				HashMap<String, String> properties5 = new HashMap<String, String>();
				properties5.put("header", "news1 from sun-sport");
				theSun.publishMessage("sun.sport", "Message about sports from "
				        + theSun.getName(), properties5);
				HashMap<String, String> properties = new HashMap<String, String>();
//				properties.put("author", "John Smith");
//				properties.put("date", "November 09");
//				properties.put("music", "rock");
				properties.put("header", "news1 from sun-music");
				theSun.publishMessage("sun.music", "Message about music from "
				        + theSun.getName(), properties);
				HashMap<String, String> properties7 = new HashMap<String, String>();
//				properties7.put("author", "Paul Taylor");
//				properties7.put("date", "November 09");
//				properties7.put("music", "rock");
				properties7.put("header", "news2 from sun-music");
				theSun.publishMessage("sun.music", "Another message about music "
				        + "from " + theSun.getName(), properties7);
				HashMap<String, String> properties6 = new HashMap<String, String>();
				properties6.put("header", "news1 from sun-arts");
				theSun.publishMessage("sun.arts", "Message about arts from "
				        + theSun.getName(), properties6);
			} catch (JMSException e) {
				e.printStackTrace();
			}

            getServletContext().getRequestDispatcher("/jsp/readerMainView.jsp")
                    .forward(request, response);
        }

    }

}
